//
//  ReturnAlertView.m
//  TengyunStation
//
//  Created by Johnny on 15/9/8.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "ReturnAlertView.h"
#import "DSToast.h"
#import "TSHeader.h"

#define ViewWidth           272.0
#define ViewHeight          96
#define ButtonHeight        44.0
#define AnimateDuration     0.25
#define LineWidth           248.0
#define ControlWidth        214.0


@implementation ReturnAlertView

- (id)initWithDelegate:(id<ReturnAlertViewDelegate>)delegate {
    self = [super initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width - ViewWidth)/2.0, ([UIScreen mainScreen].bounds.size.height - ViewHeight)/2.0, ViewWidth, ViewHeight)];
    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 5;
    self.clipsToBounds = YES;
    self.delegate = delegate;
    if (self) {
        UIImageView *expressImage = [[UIImageView alloc] initWithFrame:CGRectMake(12, 12, 20, 20)];
        expressImage.image = [UIImage imageNamed:@"kuaid_icon_erw"];
        [self addSubview:expressImage];
        _expressIdTextField = [[UITextField alloc] initWithFrame:CGRectMake(expressImage.frame.origin.x + expressImage.frame.size.width + 12, 0, ControlWidth, 44)];
        _expressIdTextField.backgroundColor = [UIColor clearColor];
        _expressIdTextField.borderStyle = UITextBorderStyleNone;
        _expressIdTextField.placeholder = @"请输入运单号";
        _expressIdTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        _expressIdTextField.delegate = self;
        _expressIdTextField.returnKeyType = UIReturnKeyNext;
        _expressIdTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _expressIdTextField.font = [UIFont systemFontOfSize:15];
        _expressIdTextField.textColor = MainTextColor;
        _expressIdTextField.autocorrectionType = UITextAutocorrectionTypeNo;
        [self addSubview:_expressIdTextField];
        UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(12, 44, LineWidth, 0.5)];
        line.backgroundColor = LineColor;
        [self addSubview:line];
//        [self creatButtonY:(ViewHeight - ButtonHeight) CancelButtonTitle:@"取消" sureButtonTitle:@"确定"];
        UILabel *line3 = [[UILabel alloc] initWithFrame:CGRectMake(0, line.frame.origin.y + 8, ViewWidth, 0.5)];
        line3.backgroundColor = LineColor;
        [self addSubview:line3];
        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        cancelBtn.frame = CGRectMake(0, line.frame.origin.y + 8, ViewWidth/2.0, 44);
        cancelBtn.backgroundColor = [UIColor clearColor];
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancelBtn setTitleColor:MainTextColor forState:UIControlStateNormal];
        cancelBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        cancelBtn.tag = 100;
        [cancelBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:cancelBtn];
        UILabel *line4 = [[UILabel alloc] initWithFrame:CGRectMake(line3.frame.size.width/2.0, line3.frame.origin.y, 0.5, 44)];
        line4.backgroundColor = LineColor;
        [self addSubview:line4];
        UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        sureBtn.frame = CGRectMake(ViewWidth/2.0, line.frame.origin.y + 8, ViewWidth/2.0, 44);
        sureBtn.backgroundColor = [UIColor clearColor];
        [sureBtn setTitle:@"确认" forState:UIControlStateNormal];
        [sureBtn setTitleColor:OrangeColor forState:UIControlStateNormal];
        sureBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        sureBtn.tag = 101;
        [sureBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:sureBtn];
    }
    return self;
}

- (void)buttonAction:(UIButton *)button {
    if (button.tag == 100) {
        [self removeReceiverAlertView];
        if ([_delegate respondsToSelector:@selector(cancelReturnAlertInput)]) {
            [_delegate cancelReturnAlertInput];
        }
    } else if (button.tag == 101) {
        if (_expressIdTextField.text.length <= 0) {
            [DSToast showText:@"请输入运单号" InView:self];
            return;
        } else if ([self.codeList containsObject:_expressIdTextField.text]) {
            [DSToast showText:@"该运单已退件，请重新输入。" InView:self];
            return;
        } else {
            if ([_delegate respondsToSelector:@selector(sureReturnAlertViewExpressId:)]) {
                [_delegate sureReturnAlertViewExpressId:_expressIdTextField.text];
                [self removeReceiverAlertView];
            }
        }
    }
}

//移除alertWindow
- (void)removeReceiverAlertView {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    [self endEditing:YES];
    [_alertWindow resignFirstResponder];
    [_alertWindow resignKeyWindow];
    _alertWindow.alpha = 0;
    _alertWindow = nil;
}

- (void)show {
    if (!_alertWindow) {
        _alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _alertWindow.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        _alertWindow.windowLevel = UIWindowLevelNormal;
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKeyboard:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyboard:) name:UIKeyboardWillHideNotification object:nil];
    [_alertWindow addSubview:self];
    [_expressIdTextField becomeFirstResponder];
    [_alertWindow makeKeyAndVisible];
}

#pragma mark UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ExpressIdLimit] invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    if (![string isEqualToString:filtered]) {
        return NO;
    }
    return YES;
}

#pragma mark Notification
- (void)showKeyboard:(NSNotification *)notification {
    NSDictionary *userInfo = notification.userInfo;
    CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    float Y = [UIScreen mainScreen].bounds.size.height - keyboardSize.height - self.frame.size.height;
    if (Y < 0) {
        Y = 0;
    }
    [UIView animateWithDuration:AnimateDuration animations:^{
        self.frame = CGRectMake(self.frame.origin.x, Y/2.0, self.frame.size.width, self.frame.size.height);
    }];
}

- (void)hideKeyboard:(NSNotification *)notification {
    [UIView animateWithDuration:AnimateDuration animations:^{
        self.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - self.frame.size.width)/2.0, ([UIScreen mainScreen].bounds.size.height - self.frame.size.height)/2.0, self.frame.size.width, self.frame.size.height);
    }];
}

@end
